package cn.xf.dao;

import cn.xf.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author xiaofu
 * @date 2021-05-17 21:02
 * @email dearaf@163.com
 */
public interface IRoleDao {

    //根据用户id查询出所有对应的角色
    @Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Results( {
            @Result(id=true,column="id",property="id"),
            @Result(column="roleName",property="roleName"),
            @Result(column="roleDesc",property="roleDesc"),
            @Result(column="id",property="permissions",javaType=List.class,
                    many=@Many(select="cn.xf.dao.IPermissionDao.findByRoleId")) })
    List<Role> findRoleByUserId(String userId);

    @Select("select * from role")
    List<Role> findAll();


    @Insert("insert into role(roleName,roleDesc) values (#{roleName},#{roleDesc})")
    void save(Role role);

    @Select("select * from role where id not in( select roleId from users_role where userId=#{id})")
    List<Role> findOtherRole(String id);

    @Select("select * from role where id = #{roleId}")
    Role findById(String roleId);

    @Insert("insert into role_permission(permissionId,roleId) values (#{permissionId},#{roleId})")
    void addPermissionToRole(@Param("roleId") String roleId, @Param("permissionId") String permissionId);
}
